﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace chlnge
{
    class Product
    {
        private int ProductId;
        private int VendorId;
        private string Name;
        private int MeasureId;
        private decimal ProductPrice;
        private int CurrencyId;

        public Product(int productId, int vendorId, string name, int measureId, decimal productPrice, int currencyId)
        {
            this.ProductId = productId;
            this.VendorId = vendorId;
            this.Name = name;
            this.MeasureId = measureId;
            this.ProductPrice = productPrice;
            this.CurrencyId = currencyId;
        }

        public int PProductId
        {
            get
            {
                return ProductId;
            }
            set
            {
                ProductId = value;
            }
        }

        public int PVendorId
        {
            get
            {
                return VendorId;
            }
            set
            {
                VendorId = value;
            }
        }

        public string PName
        {
            get
            {
                return Name;
            }
            set
            {
                Name = value;
            }
        }

        public int PMeasureId
        {
            get
            {
                return MeasureId;
            }
            set
            {
                MeasureId = value;
            }
        }

        public decimal PProductPrice
        {
            get
            {
                return ProductPrice;
            }
            set
            {
                ProductPrice = value;
            }
        }

        public int PCurrencyId
        {
            get
            {
                return CurrencyId;
            }
            set
            {
                CurrencyId = value;
            }
        }

        public static List<Product> GetProducts() 
        {
            string connString = "Server = localhost; Database = productsinformation; Uid = root; password = valerieva";

            MySqlConnection conn = new MySqlConnection(connString);
            List<Product> products = new List<Product>();
            MySqlCommand command = conn.CreateCommand();
            command.CommandText = GenerateQuerry();
            using (conn)
            {
                conn.Open();
                MySqlDataReader reader = command.ExecuteReader();
                using (reader)
                {
                    while (reader.Read())
                    {
                        products.Add(new Product((int)reader["ProductId"], (int)reader["VendorId"], (string)reader["Name"],
                                                 (int)reader["MeasureId"], (decimal)reader["ProductPrice"],
                                                 (int)reader["CurrencyId"]));
                    }
                }
            }
            return products;
        }

        private static string GenerateQuerry() 
        {
            return string.Format("Select * From products");
        }
    }
}
